from apscheduler.schedulers.asyncio import AsyncIOScheduler
from src.utils.logger import app_logger as logger
from src.services.checkin_service import CheckinService


class SchedulerService:
    """定时任务服务"""
    
    def __init__(self):
        self.scheduler = AsyncIOScheduler()
        self._setup_jobs()
    
    def _setup_jobs(self):
        """设置定时任务"""
        # 每天凌晨0点1分重置打卡状态
        self.scheduler.add_job(
            CheckinService.reset_daily_checkin,
            'cron',
            hour=0,
            minute=1,
            id='reset_daily_checkin',
            name='重置每日打卡状态',
            replace_existing=True
        )
        logger.info("定时任务已配置：每日凌晨0点1分重置打卡状态")
    
    def start(self):
        """启动定时任务"""
        if not self.scheduler.running:
            self.scheduler.start()
            logger.info("定时任务调度器已启动")
    
    def stop(self):
        """停止定时任务"""
        if self.scheduler.running:
            self.scheduler.shutdown()
            logger.info("定时任务调度器已停止")
    



# 全局调度器实例
scheduler_service = SchedulerService()